(function (G) {
    G.vue({
        data: {
            LIST_count:3,
            LIST_index:0,
            userId:'',
            type:'',
            code:1,
            APIURL:G.Storage.get('APIURL'),
            status: ['待处理', '处理记录','我发起']
        },
        methods: {
            getInstruction(f_taskId,next){
                G.get('/api/model/instruction', { f_taskId }).then(res => {
                    if(!res.errorMsg){
                        res[0] = res[0] || []
                        let newClickedList = []
                        let imgs = res[0].images ? res[0].images.split(',') : []
                        for(let i = 0; i < imgs.length; i ++){
                            newClickedList.push({
                                address: res[0].address,
                                latitude: res[0].latitude,
                                longitude: res[0].longitude,
                                showUrl: imgs[i].replace('/file/',''),
                                url: imgs[i]
                            })
                        }
                        console.log(newClickedList)
                        G.Storage.set('newClickedList',newClickedList)
                        next(res[0])
                    }else{
                        G.task(res.errorMsg)
                    }
                })
            },
            go(event){
                let item = event.currentTarget.dataset.item, that = this
                if(this.data.type == 'zlxd'){
                    this.getInstruction(item.id,instruction => {
                        if(item.canDo){
                            if(item.statusCode == 20){
                                wx.navigateTo({
                                    url: '/pages/gycase/addAndKoufen/index?type=2&zlxd=1',
                                    events: {
                                        addRecord: function (data) {
                                            G.put('/api/model/instruction',{id:instruction.id, taskId:data.taskId}).then(res => {
                                                //将案件设置为领导指令下达
                                                G.put('/api/model/f_task', {
                                                    id:data.taskId,
                                                    expand1: 'zlxd_' + item.id
                                                }).then(res => {
                                                    //更新流程进度到责任人处理
                                                    G.put('/v1/task/update',{ 
                                                        taskId:item.id
                                                     }).then(res => {
                                                        that.LIST_refresh(that.getData('LIST_index'))
                                                    })
                                                })
                                            })
                                        }
                                    }
                                })
                            }else if(item.statusCode == 40){
                                wx.showActionSheet({
                                    alertText:'选择考核类型',
                                    itemList: ['查看案件处理情况','月度巡查考核', '公众监督考核', '重大事件考核'],
                                    success (res) {
                                        let tapIndex = res.tapIndex
                                        if(tapIndex == 0){
                                            G.$go(`/pages/flow/detail/index?taskId=${instruction.taskId}`)
                                            return false
                                        }
                                        wx.navigateTo({
                                            url: '/pages/check/write/index?type=' + (tapIndex + 1),
                                            events: {
                                                addRecord: function (data) {
                                                    //更新扣分ID到指令下达
                                                    G.put('/api/model/instruction',{id:instruction.id, assessId:data.id}).then(res => {
                                                        //更新流程进度到完结
                                                        G.put('/v1/task/update',{
                                                            taskId:item.id
                                                            //差查看处理结果
                                                        }).then(res => {
                                                            that.LIST_refresh(that.getData('LIST_index'))
                                                        })
                                                    })
                                                }
                                            }
                                        })
                                    },
                                    fail (res) {
                                      console.log(res.errMsg)
                                    }
                                })
                            }else{
                                wx.showActionSheet({
                                    alertText:'选择',
                                    itemList: ['查看案件处理情况','查看流程进度'],
                                    success (res) {
                                        let tapIndex = res.tapIndex
                                        G.$go(`/pages/flow/detail/index?taskId=${tapIndex == 0 ? instruction.taskId : item.id}`)
                                    },
                                    fail (res) {
                                      console.log(res.errMsg)
                                    }
                                })
                            }
                        }else if(item.statusCode == 20){
                            G.$go(`/pages/flow/detail/index?taskId=${item.id}`)
                        }else{
                            wx.showActionSheet({
                                alertText:'选择',
                                itemList: ['查看案件处理详情','查看指令流程进度'],
                                success (res) {
                                    let tapIndex = res.tapIndex
                                    G.$go(`/pages/flow/detail/index?taskId=${tapIndex == 0 ? instruction.taskId : item.id}`)
                                },
                                fail (res) {
                                  console.log(res.errMsg)
                                }
                            })
                        }
                    })
                }else{
                    G.$go(`/pages/flow/detail/index?taskId=${item.id}`)
                }
            },
            changeActive(e){
                this.setData({LIST_index:e.currentTarget.dataset.index})
            },
            onReachBottom() {
                this.LIST_load(this.getData('LIST_index'))
            }
        },
        mounted(query) {
            let userId = G.Storage.get('userInfo').userId
            this.setData({
                userId,
                code:query.code == 1 ? 1 : 0,
                LIST_index: query.code == 1 ? 0 : 1,
                type:query.type
            })
            wx.setNavigationBarTitle({
              title: query.name + (query.code == 1 ? '待完成' : '处理记录'),
            })
            function buildList(type){
                return G.list({
                    dataFormat(row){
                      //row.images = row.images.split(',')
                      row.canDo = row.toUsers && row.toUsers.indexOf(userId) >= 0 && row.status == 1 ? true : false
                      row.createTime = row.time || G.date(row.createTime).Format('yyyy-MM-dd')
                    },
                    get:{url:'/v1/task/list',data:{flowType:query.type,type}}
                })
            }
            this.LIST_init([
                buildList(1),
                buildList(2),
                buildList(3)
            ])
            this.LIST_loads()
        }
    })
})(wx.$)